﻿package kliment.utils {
	/**
	 * ...
	 * @author	Kliment
	 * @version	1.0.5
	 *
	 * class name: kliment.utils._Array
	 */
	public class _Array	{

		public static function inArray(value:*, target:Array):Boolean
		{
			return target.indexOf(value) > -1;
		}

		public static function remove(value, target:Array):uint {
			var count = 0;
			var i = target.length;
			while (-1 < (i = target.indexOf(value))) {
				target.splice(i, 1);
				count += 1;
			}

			return count;
		}

		public static function last(value:Array):* {
			return value[lastIndex(value)];
		}

		public static function lastIndex(value:Array):Number {
			return value.length - 1;
		}

		public static function copy(value:Array):Array {
			var result:Array = new Array();
			for (var i:* in value) {
				result[i] = value[i];
			}
			return result
		}

		public static function subArray(value:Array, startIndex:uint = 0, endIndex:uint = 4294967295):Array {
			var result:Array = new Array();
			if (endIndex > value.length)
				endIndex = value.length - 1;
			if (startIndex > endIndex)
				return result;
			for (startIndex; startIndex <= endIndex; ++startIndex) {
				result.push(value[startIndex]);
			}
			return result;
		}

		public static function multiply(value:Number, array:Array):Array {
			var temp:Array = new Array();
			for each(var item:Number in array) {
				if(item == 0)
					temp.push(0);
				else
					temp.push(value * item);
			}
			return temp;
		}


		static public var sortCondition:Function = function(val1:*, val2:*):Boolean { return false };

		static public function sortInsertion(array:Array, sortCondition:Function):Array {
			var length:int = array.length;
			var temp:Array = new Array();
			for (var i:int = 0; i < length; i++){
				var j:int = i;
				while (j > 0 && sortCondition(temp[j-1], array[i])){
					temp[j] = temp[j-1];
					j--;
				}
				temp[j] = array[i];
			}
			return temp;
		}

		static public function leftAndRightDelta(leftArray:Array, rightArray:Array):Object {
			var isThere:Boolean;
			var lengthLeft:int = leftArray.length;
			var lengthRight:int = rightArray.length;
			var left:Array = new Array();
			var right:Array = new Array();
			var i:int;
			var j:int;

			for (i = 0; i < lengthLeft; i++) {
				isThere = false;
				for (j = 0; j < lengthRight; j++) {
					if (leftArray[i] == rightArray[j]) {
						isThere = true;
						break;
					}
				}
				if (!isThere)
					left.push(leftArray[i]);
			}

			for (j = 0; j < lengthRight; j++) {
				isThere = false;
				for (i = 0; i < lengthLeft; i++) {
					if (leftArray[i] == rightArray[j]) {
						isThere = true;
						break;
					}
				}
				if (!isThere)
					right.push(rightArray[j]);
			}

			return {left:left, right:right};
		}

		static public function makeUnique(target:Array):void
		{
			var index:int;
			var i:int = 0;
			while (i < target.length)
			{
				index = target.indexOf(target[i], i + 1);
				if (index > -1)
				{
					target.splice(index, 1);
					continue;
				}

				i++;
			}
		}
	}

}